import { queryLogin, queryMultipart, queryRegister } from '@/api/goods'
import { SET_TOKEN, GET_TOKEN, REMOVE_TOKEN } from '@/utils/userToken'
import store from '..'
export default {
  namespaced: true,
  state: {
    token: GET_TOKEN() || '',
    userInfo: {},
    as: false
  },
  getters: {
  },
  mutations: {
    SET_TOKEN (state, data) {
      state.token = data
    },
    SET_USERINFO (state, info) {
      state.userInfo = info
    },
    // 退出登录
    RESET_STATE (state) {
      state.token = ''
      state.userInfo = {}
      REMOVE_TOKEN()
    },
    xg (state) {
      state.as = true
    },
    xg1 (state) {
      state.as = false
    }
  },
  actions: {
    async login ({ commit, dispatch }, data) {
      const res = await queryLogin(data)
      commit('SET_TOKEN', res.token)
      SET_TOKEN(res.token)
      dispatch('getUserInfo')
    },
    async getUserInfo ({ commit }) {
      const res = await queryMultipart()
      commit('SET_USERINFO', res)
    },
    logout ({ commit }) {
      store.token = ''
      store.userInfo = {}
      commit('RESET_STATE')
    },
    async register ({ commit }, data) {
      await queryRegister(data)
    }
  },
  modules: {
  }
}
